package com.tms.app.tms.ui.home.history;


import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tms.app.tms.adapter.HistoryAdapter;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.TaskInfo;
import com.tms.app.tms.bean.response.HistoryTaskRes;
import com.tms.app.tms.bean.response.TaskRes;
import com.tms.app.tms.db.TaskInfoDBUtil;
import com.tms.app.tms.ui.base.presenter.AbsServicexUtils;
import com.tms.app.tms.ui.base.view.IListView;
import com.tms.app.tms.ui.base.presenter.AbsBaseListP;
import com.tms.app.tms.bean.TestBean;
import com.tms.app.tms.ui.taskinfo.TaskInfoActivity;

import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by happy on 2017/8/28.
 */

public class HistoryP extends AbsBaseListP {
    IListView iViewTrade;
    private int pageIndex=1;
    private int totalCount=0;
    private int requestType=1;
    private String token;
    private String search;

    public void setiView(IListView iViewTrade) {
        this.iViewTrade = iViewTrade;
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        HistoryTaskRes taskRes=gson.fromJson(result,HistoryTaskRes.class);
        bindDataToView(taskRes);
    }

    @Override
    public void onFinished() {
        super.onFinished();
    }

    private void bindDataToView(HistoryTaskRes taskRes){
        if (iViewTrade==null)
            return;
        HistoryAdapter depthAdapter= iViewTrade.getAdapter(0);
        if (requestType==1)
            depthAdapter.getData().clear();
        if (taskRes==null||taskRes.getCode()!=200||taskRes.getData()==null
                ||taskRes.getData().getList()==null||taskRes.getData().getList().size()<1){
            iViewTrade.refreshNoData();
            iViewTrade.pullMoreNoMore();
            return;
        }
        TaskInfoDBUtil.saveOrUpadateTask(taskRes.getData().getList(),2);
      //  depthAdapter.getData().addAll(TaskInfoDBUtil.getHistoryTaskList());
        depthAdapter.getData().addAll(taskRes.getData().getList());
        depthAdapter.notifyDataSetChanged();
        if (requestType==2)
            pageIndex++;
        totalCount=taskRes.getCode();
        iViewTrade.refreshComplite();
        iViewTrade.pullMoreComlite();
    }




    public void getData(String search){
        this.requestType=1;
        pageIndex=1;
        getData(token,pageIndex,search);
    }

    public void getData(String token, int pageIndex,String search){
        this.token=token;
        this.search =search;
        RequestParams params=new RequestParams(Constant.TaskHistory);
        params.addBodyParameter("token",token);//
        params.addBodyParameter("pageSize","20");
        params.addBodyParameter("q",search);
        params.addBodyParameter("pageIndex",pageIndex+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        super.onItemClick(adapter, view, position);
        TaskInfo taskInfo= (TaskInfo) adapter.getData().get(position);
        Intent intent=new Intent(view.getContext(),TaskInfoActivity.class);
        intent.putExtra("planId",taskInfo.getAllotPlanId());
        intent.putExtra("taskType",4);
        iViewTrade.startActivity(intent);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        requestType=1;
        pageIndex=1;
        getData(token,pageIndex,search);
    }

    @Override
    public void onLoadMoreRequested() {
        super.onLoadMoreRequested();
        HistoryAdapter depthAdapter= iViewTrade.getAdapter(0);
        if (depthAdapter.getData().size()>=totalCount){
            iViewTrade.pullMoreNoMore("");
        }
        requestType=2;
        getData(token,pageIndex+1,search);
    }
}
